﻿using Kenly.DBFramework.Reflection;
using Kenly.DBFramework.Gateway;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Reflection;

using Kenly.DBFramework.TestModel.HumanResources;

namespace Kenly.DBFramework.Tests
{


    /// <summary>
    ///这是 IdentityUtilityTest 的测试类，旨在
    ///包含所有 IdentityUtilityTest 单元测试
    ///</summary>
    [TestClass()]
    public class IdentityUtilityTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///获取或设置测试上下文，上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试属性
        // 
        //编写测试时，还可使用以下属性:
        //
        //使用 ClassInitialize 在运行类中的第一个测试前先运行代码
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //使用 ClassCleanup 在运行完类中的所有测试后再运行代码
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //使用 TestInitialize 在运行每个测试前先运行代码
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //使用 TestCleanup 在运行完每个测试后运行代码
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///SetIdentityValue 的测试
        ///</summary>
        [TestMethod()]
        public void SetIdentityValueTest()
        {
            Employee entity = new Employee { EmployeeID = 1 };

            IdentityUtility.SetIdentityValue(entity, 2);
            Assert.IsTrue(entity.EmployeeID == 2);

            IViewGateway view = new Employee { EmployeeID = 1 };

            IdentityUtility.SetIdentityValue(view, 2);
            Assert.IsTrue(view.EntityId.ToString() == "2");

            IdentityUtility.SetIdentityValue(null, 2);
            IdentityUtility.SetIdentityValue(new object(), 2);
        }

        /// <summary>
        ///GetIdentityName 的测试
        ///</summary>
        [TestMethod()]
        public void GetIdentityNameTest()
        {
            string actual = IdentityUtility.GetIdentityName(null);
            Assert.IsTrue(string.IsNullOrEmpty(actual));

            actual = IdentityUtility.GetIdentityName(typeof(object));
            Assert.IsTrue(string.IsNullOrEmpty(actual));

            actual = IdentityUtility.GetIdentityName(typeof(Employee));
            Assert.IsTrue(actual == "EmployeeID");
        }

        /// <summary>
        ///GetIdentity 的测试
        ///</summary>
        [TestMethod()]
        public void GetIdentityTest()
        {
            Employee entity = new Employee { EmployeeID = 1 };
            string identityName;
            object identityValue;

            IdentityUtility.GetIdentity(null, out identityName, out identityValue);
            Assert.IsTrue(string.IsNullOrEmpty(identityName));
            Assert.IsNull(identityValue);

            IdentityUtility.GetIdentity(new object(), out identityName, out identityValue);
            Assert.IsTrue(string.IsNullOrEmpty(identityName));
            Assert.IsNull(identityValue);

            IdentityUtility.GetIdentity(entity, out identityName, out identityValue);
            Assert.IsTrue(identityName == "EmployeeID");
            Assert.IsTrue(identityValue.ToString() == "1");
        }

        /// <summary>
        ///GetIdentityProperty 的测试
        ///</summary>
        [TestMethod()]
        public void GetIdentityPropertyTest()
        {
            PropertyInfo actual = IdentityUtility.GetIdentityProperty(null);
            Assert.IsNull(actual);

            actual = IdentityUtility.GetIdentityProperty(typeof(object));
            Assert.IsNull(actual);

            actual = IdentityUtility.GetIdentityProperty(typeof(Employee));
            Assert.IsTrue(actual.Name == "EmployeeID");
        }
    }
}
